﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.DanhMucChung
{
    public partial class CategoryLookup : FormPopupBase
    {
        private readonly NhomHangBO service = new NhomHangBO();
        public NhomHang CategorySelected;

        public CategoryLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }

      
        void InitStyle()
        {
            grvCategoryList.OptionsBehavior.Editable = false;
            grvCategoryList.OptionsView.ShowGroupPanel = false;
            grvCategoryList.OptionsView.ShowIndicator = true;

            grvCategoryList.OptionsView.ShowAutoFilterRow = true;
            grvCategoryList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvCategoryList.OptionsView.ShowGroupedColumns = false;
            grvCategoryList.OptionsMenu.EnableColumnMenu = false;
            grvCategoryList.OptionsCustomization.AllowColumnMoving = false;
            grvCategoryList.OptionsCustomization.AllowGroup = false;
            grvCategoryList.OptionsFilter.AllowFilterEditor = false;

            this.grvCategoryList.OptionsSelection.MultiSelect = false;
            this.grvCategoryList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colCode.FieldName = NhomHangColumns.MaNH.ToString();
            this.colName.FieldName = NhomHangColumns.TenNH.ToString();
            this.colNotes.FieldName = NhomHangColumns.GhiChu.ToString();

        }

        void InitData()
        {
            try
            {
                this.grdCategoryList.DataSource = service.GetAllNhomHang(); 
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.CategorySelected = (NhomHang)grvCategoryList.GetFocusedRow();
                }
                else
                {
                    this.CategorySelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvCategoryList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvCategoryList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdCategoryList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (CategorySelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvCategoryList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvCategoryList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            CategorySelected = this.grvCategoryList.GetFocusedRow() as NhomHang;
        }

        private void grvCategoryList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
